<?
	$tpl->draw("header");
	$tpl->draw("navigator");

	if($year == ""){
		$year = date("Y");
	}

	$start_year = mktime(0, 0, 0, 1, 1, $year);
	$end_year = mktime(0, 0, 0, 1, 0, $year+1);

	$all_date = array();
	for($t = $start_year; $t <= $end_year; $t += DAY){
		$all_date[date("Y-m-d",$t)] = array();
	}

	$all_event = $dsfm_calendar->get_all_event();

	foreach($all_event as $k => $v){
		if( strtotime($v['start_date']) <= $end_year && strtotime($v['stop_date']) >= $start_year){

			if( strtotime($v['start_date']) <= $start_year){
				$v['start_date'] = date("Y-m-d",$start_year);
			}

			for($t = strtotime($v['start_date']); $t <= strtotime($v['stop_date']) && $t <= $end_year; $t += DAY){

				$tmp = $all_date[date("Y-m-d", $t)];

				if($v['line'] == ""){
					$v['line'] = "line".count($tmp);
				}

				array_push($tmp, $v);

				$all_date[date("Y-m-d", $t)] = $tmp;
			}
		}
	}
?>

<link href = "<?=URL?>admin/module/calendar/style.css" rel='stylesheet' type='text/css' />

<script>
	$(document).ready(function() {
		$(".pass").html("");
	});
</script>

<ul class="table-menu">
	<li class = "fl">
		<a href = "form.php"><img src = "<?=URL?>admin/html/images/add.png">Add Event</a>
	</li>
	<li class = "fr">
		<a href = "?year=<?=$year-1?>"><img src = "<?=URL?>admin/html/images/previous.png">Previous</a>
		<?=$year?>
		<a href = "?year=<?=$year+1?>">Next<img src = "<?=URL?>admin/html/images/next.png"></a>
	</li>
</ul>
<?
	for($m = 0; $m < 12; $m++){

		$start = mktime(0, 0, 0, 1+$m, 1, $year);
		$end = mktime(0, 0, 0, $m+2, 0, $year);
		$time = ($start - (date("N",$start)-1)*DAY);

?>
	<div class = 'month'>
		<div class = "month-name"><?=date("F", $start)?></div>
		<div class = "box head" val = "day0">จ</div>
		<div class = "box head" val = "day1">อ</div>
		<div class = "box head" val = "day2">พ</div>
		<div class = "box head" val = "day3">พฤ</div>
		<div class = "box head" val = "day4">ศ</div>
		<div class = "box head" val = "day5">ส</div>
		<div class = "box head" val = "day6">อ</div>
	<?
		$date_on_cal = 42;

		for($box=0; $box < $date_on_cal; $box++){

			$boxtime = $time+DAY*$box;

			if($boxtime < $start || $boxtime > $end){
				$class = "pass";
			}else{
				$class = "";
			}

			echo "<div class = 'box $class day".($box%7)."' val = '".$boxtime."'><span class = 'date'>".date("d",$boxtime)."</span>\n";
			if( count($all_date[date("Y-m-d", $boxtime)]) != 0){

				foreach($all_date[date("Y-m-d", $boxtime)] as $k => $v){
					echo "<a href = 'edit.php?id=".$v['id']."'><span class = 'event ".$v['line']."'>";
					echo $v['topic_th'];
					echo "</span></a>";
				}
			}
			echo "</div>\n";
		}
	?>
	</div>
<? } ?>
<div class = "clear"></div>

<?
	$tpl->draw("footer");
?>